﻿#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;

void Print(const vector<int>& v)
{
	for (size_t i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}
	cout << endl;

	/*for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int>::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it  << " ";
		++it;
	}
	cout << endl;*/

	// 迭代器
	for (auto it = v.begin(); it != v.end(); ++it)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test_vector1()
{
	vector<int> v1;           // 空vector
	//vector<int> v(5);         //包含5个元素，默认值是0
	vector<int> v2(10, 1);    // 包含10个元素，每个元素值是1
	vector<int> v3(v2.begin(), v2.end()); // 区间构造，用迭代器的左闭右开的所有元素来
	//初始化新的v3 等价于v3 = v2

	string s1("xxxxxxxxxxxxx");
	vector<int> v4(s1.begin(), s1.end());// 因为‘x’的ASCII码是120，所以每个字符都被转换成整数120
	//此时v4存的是{120 120 120 120 120 120 120 120 120 120 120 120 120}
	//属于是char->int的类型自动提升
	//示例：
	vector<int> v = { 65,66,67 };
	string s(v.begin(), v.end());
	//通过编译之后，s里面其实是“ABC”
	
	vector<int> v5(v3);


	//vector<int> v6({ 1,2,3,4,5 }); //等价于下面这个
	vector<int> v6 = { 1,2,3,4,5 };
	vector<int> v7 = { 1,2,3,4,5,1,1,1,1,1,1 };

	Print(v2);
	Print(v4);
	Print(v6);
	Print(v7);

	auto i1 = { 10,20,30,1,2,2 }; // i1的实际类型 既不是数组 也不是vector 而是std::initializer_list<int>
	for (auto e : i1)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector2()
{
	vector<int> v1;
	// const int n = 100000000;
	const int n = 100;
	// v1.reserve(n);

	size_t old = v1.capacity();
	// cout << v1.capacity() << endl;
	size_t begin = clock();
	for (size_t i = 0; i < n; i++) 
	{
		v1.push_back(i);
	}
	size_t end = clock();
	cout << end - begin << endl;

	vector<int> v2;
	v2.resize(100, 1);
	Print(v2);
}
struct AA
{
	int _a1 = 1;
	int _a2 = 1;

	AA(int a1 = 1, int a2 = 1)
		:_a1(a1)
		, _a2(a2)
	{
	}
};

void test_vector4()
{
	AA aa1 = { 0, 0 };
	vector<AA> v = { aa1, {1,1}, {2,2}, {3,3} };
	auto it = v.begin();
	while (it != v.end())
	{
		cout << it->_a1 << ":" << it->_a2 << endl;
		++it;
	}
	cout << endl;

	v.push_back(aa1);
	v.emplace_back(aa1);

	v.emplace_back(1, 1); // 就地构造
	v.push_back({ 2,2 });// 多个一个临时对象

	it = v.begin();
	while (it != v.end())
	{
		cout << it->_a1 << ":" << it->_a2 << endl;
		++it;
	}
	cout << endl;
}